<%
' ---------------------------------------------------------------------------------------
' ----------  Emulator for Proliphix thermostats
' ----------
' ----------  This driver allows one to develop against the Proliphix API using a standard WIndows XP
' ----------  machine without requiring access to a functioning Proliphix thermostat.
' ----------  This Proliphix driver is available on my blog, along with information on how to use it:
' ----------  http://wiredright.wordpress.com/
' ----------
' ----------  Written by: Josh Kewley
' ----------              Insario Corporation
' ----------  Version:    4.2.2008
' ---------------------------------------------------------------------------------------
%>
<!--#include file="proliphixConsts.asp"-->
<!--#include file="proliphixMethods.asp"-->
<%
' Proliphix Rules:'
' Shouldnt poll more than once a minute'
' After a set request, wait at least one second to issue a get to confirm'
' FAILED5 response indicates bad OID sent to get '
' FAILED6 response indicates bad OID sent to set, or trying to set read-only value'
' FAILED8 response indicates bad OID sent to set, to get, or trying to set read-only value'
' Setback heat should not be set within 2 degrees of setback cool if HVAC mode is set to auto'
' Dont set Active Period - it is computed based upon time of day and schedule '
' Device may return "please try again" if web server is busy'

InitApplication()

if (Ucase(Request("ACTION"))="PDP") then
    'Response.Write "PDP"
    RunSetRoutine()
elseif (Ucase(Request("ACTION"))="GET") then
    'Response.Write "GET"
    RunGetRoutine()
else
    Response.Write "Test driver requires a querystring id of ACTION with value ""get"" or ""pdp"""
end if


Sub InitApplication()
    if (Application(MODE)="") then Application(MODE)=MODE_COOL
    if (Application(STATE)="") then Application(STATE)=STATE_COOL
    if (Application(FAN)="") then Application(FAN)=FAN_AUTO
    if (Application(FANSTATE)="") then Application(FANSTATE)=FANSTATE_ON
    if (Application(SETBACKHEAT)="") then Application(SETBACKHEAT)="500"
    if (Application(SETBACKCOOL)="") then Application(SETBACKCOOL)="900"
    if (Application(SETBACKSTATUS)="") then Application(SETBACKSTATUS)=SETBACKSTATUS_NORMAL
    if (Application(CURRENTPERIOD)="") then Application(CURRENTPERIOD)=CURRENTPERIOD_MORN
    if (Application(CURRENTCLASS)="") then Application(CURRENTCLASS)=CURRENTCLASS_OCCUPIED
    if (Application(ACTIVEPERIOD)="") then Application(ACTIVEPERIOD)=ACTIVEPERIOD_MORN
    if (Application(SENSOR_NAME_1)="") then Application(SENSOR_NAME_1)="OUTSIDE FRONT"
    if (Application(SENSOR_AVERAGE_LOCAL)="") then Application(SENSOR_AVERAGE_LOCAL)=SENSOR_AVERAGE_ENABLED
    if (Application(SENSOR_AVERAGE_1)="") then Application(SENSOR_AVERAGE_1)=SENSOR_AVERAGE_DISABLED
    if (Application(SENSOR_AVERAGE_2)="") then Application(SENSOR_AVERAGE_2)=SENSOR_AVERAGE_DISABLED
    if (Application(SENSOR_STATE_LOCAL)="") then Application(SENSOR_STATE_LOCAL)=SENSOR_ENABLED
    if (Application(SENSOR_STATE_1)="") then Application(SENSOR_STATE_1)=SENSOR_ENABLED
    if (Application(SENSOR_STATE_2)="") then Application(SENSOR_STATE_2)=SENSOR_DISABLED
    if (Application(SENSOR_TYPE_1)="") then Application(SENSOR_TYPE_1)=THERMISTOR_SENSOR
    if (Application(AVERGAE_TEMP)="") then Application(AVERGAE_TEMP)="777"
    if (Application(SENSOR_TEMP_LOCAL)="") then Application(SENSOR_TEMP_LOCAL)="770"
    if (Application(SENSOR_TEMP_1)="") then Application(SENSOR_TEMP_1)="950"
    if (Application(SYSTEM_UPTIME)="") then Application(SYSTEM_UPTIME)=GetUptime()
    if (Application(SYSTEM_TIME_SECS)="") then Application(SYSTEM_TIME_SECS)=GetTime()
    if (Application(DEVICE_NAME)="") then Application(DEVICE_NAME)=thisRoomName
    if (Application(MODEL_NUMBER)="") then Application(MODEL_NUMBER)="NT20"
    if (Application(RELATIVE_HUMIDITY)="") then Application(RELATIVE_HUMIDITY)="25"
End Sub

Sub RunSetRoutine()
    if (Request("submit")="") then
        Response.Write "SETS require the value submit=SUBMIT"
        Exit Sub
    end if
    if IsPresentForSet(MODE) then SetApplication(MODE)
    if IsPresentForSet(FAN) then SetApplication(FAN)
    if IsPresentForSet(SETBACKHEAT) then SetApplication(SETBACKHEAT)
    if IsPresentForSet(SETBACKCOOL) then SetApplication(SETBACKCOOL)
    if IsPresentForSet(SETBACKSTATUS) then SetApplication(SETBACKSTATUS)
    if IsPresentForSet(CURRENTPERIOD) then SetApplication(CURRENTPERIOD)
    if IsPresentForSet(SENSOR_NAME_1) then SetApplication(SENSOR_NAME_1)
    if IsPresentForSet(SENSOR_STATE_LOCAL) then SetApplication(SENSOR_STATE_LOCAL)
    if IsPresentForSet(SENSOR_STATE_1) then SetApplication(SENSOR_STATE_1)
    if IsPresentForSet(SENSOR_STATE_2) then SetApplication(SENSOR_STATE_2)
    if IsPresentForSet(SENSOR_TYPE_1) then SetApplication(SENSOR_TYPE_1)
    if IsPresentForSet(DEVICE_NAME) then SetApplication(DEVICE_NAME)
    'THE FOLLOWING ARE  READ ONLY, but they've been left setable for testing purposes:
                    if IsPresentForSet(STATE) then SetApplication(STATE)
                    if IsPresentForSet(FANSTATE) then SetApplication(FANSTATE)
                    if IsPresentForSet(CURRENTCLASS) then SetApplication(CURRENTCLASS)
                    if IsPresentForSet(ACTIVEPERIOD) then SetApplication(ACTIVEPERIOD)
                    if IsPresentForSet(AVERGAE_TEMP) then SetApplication(AVERGAE_TEMP)
                    if IsPresentForSet(SENSOR_TEMP_LOCAL) then SetApplication(SENSOR_TEMP_LOCAL)
                    if IsPresentForSet(SENSOR_TEMP_1) then SetApplication(SENSOR_TEMP_1)
                    if IsPresentForSet(MODEL_NUMBER) then SetApplication(MODEL_NUMBER)
                    if IsPresentForSet(SYSTEM_UPTIME) then SetApplication(SYSTEM_UPTIME)
                    if IsPresentForSet(SYSTEM_TIME_SECS) then SetApplication(SYSTEM_TIME_SECS)
                    if IsPresentForSet(RELATIVE_HUMIDITY) then SetApplication(RELATIVE_HUMIDITY)
End Sub

Sub RunGetRoutine()
    if IsPresent(MODE) then WriteValue(MODE)
    if IsPresent(STATE) then WriteValue(STATE)
    if IsPresent(FAN) then WriteValue(FAN)
    if IsPresent(FANSTATE) then WriteValue(FANSTATE)
    if IsPresent(SETBACKHEAT) then WriteValue(SETBACKHEAT)
    if IsPresent(SETBACKCOOL) then WriteValue(SETBACKCOOL)
    if IsPresent(SETBACKSTATUS) then WriteValue(SETBACKSTATUS)
    if IsPresent(CURRENTPERIOD) then WriteValue(CURRENTPERIOD)
    if IsPresent(CURRENTCLASS) then WriteValue(CURRENTCLASS)
    if IsPresent(ACTIVEPERIOD) then WriteValue(ACTIVEPERIOD)
    if IsPresent(SENSOR_NAME_1) then WriteValue(SENSOR_NAME_1)
    if IsPresent(SENSOR_STATE_LOCAL) then WriteValue(SENSOR_STATE_LOCAL)
    if IsPresent(SENSOR_STATE_1) then WriteValue(SENSOR_STATE_1)
    if IsPresent(SENSOR_STATE_2) then WriteValue(SENSOR_STATE_2)
    if IsPresent(SENSOR_TYPE_1) then WriteValue(SENSOR_TYPE_1)
    if IsPresent(AVERGAE_TEMP) then WriteValue(AVERGAE_TEMP)
    if IsPresent(SENSOR_TEMP_LOCAL) then WriteValue(SENSOR_TEMP_LOCAL)
    if IsPresent(SENSOR_TEMP_1) then WriteValue(SENSOR_TEMP_1)
    if IsPresent(RELATIVE_HUMIDITY) then WriteValue(RELATIVE_HUMIDITY)
    if IsPresent(DEVICE_NAME) then WriteValue(DEVICE_NAME)
    if IsPresent(MODEL_NUMBER) then WriteValue(MODEL_NUMBER)
    if IsPresent(SYSTEM_UPTIME) then WriteValue(GetUptime())
    if IsPresent(SYSTEM_TIME_SECS) then WriteValue(GetTime())
End Sub

Function IsPresent(someQS)
  On Error Resume Next
    prefixed = "OID" & someQS
    IsPresent = ((Instr(Request.QueryString, prefixed)>0) OR (not IsEmpty(Request.QueryString(prefixed))) OR (NOT IsEmpty(Request.Form(prefixed))))
End Function

Function IsPresentForSet(someQS)
    prefixed = "OID" & someQS
    'IsPresentForSet = ((Instr(Request.QueryString, prefixed)>0) OR (Request.QueryString(prefixed)<>"") OR (Request.Form(prefixed)<>vbnull))
    IsPresentForSet = len(Request(prefixed))>0
End Function


Sub WriteValue(aKey)
    Response.Write "OID" & aKey & "=" & Application(aKey) & "&"
End Sub

%>